/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.pac3.rmi.impl;

import edu.uoc.tdp.pac3.bo.IIncidenciaBO;
import edu.uoc.tdp.pac3.bo.impl.IncidenciaBO;
import edu.uoc.tdp.pac3.configuration.IConfiguration;
import edu.uoc.tdp.pac3.configuration.PropertiesConfiguration;
import edu.uoc.tdp.pac3.domain.Area;
import edu.uoc.tdp.pac3.domain.Request;
import edu.uoc.tdp.pac3.domain.RequestNote;
import edu.uoc.tdp.pac3.domain.RequestType;
import edu.uoc.tdp.pac3.domain.User;
import edu.uoc.tdp.pac3.rmi.GestorIncidenciesInterface;
import edu.uoc.tdp.pac3.to.HistoricIncidenciaTO;
import java.rmi.AccessException;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Classe encarregada de publicar tots els metodes de IIncidenciaBO.
 * D'aquesta manera publiquem tot el Business Object que el client ha de fer servir.
 * També s'encarrega de registrar al rmic el nostre servidor.
 * @author Gerard
 */
public class GestorIncidenciesImpl extends UnicastRemoteObject implements GestorIncidenciesInterface {
    
    private Registry registry;
    
    private IIncidenciaBO incidenciaBO;

    public GestorIncidenciesImpl(IConfiguration configuration) throws RemoteException {
        try {
            registry = LocateRegistry.createRegistry(Integer.parseInt(configuration.getProperty(IConfiguration.RMI_PORT)));
            registry.bind(configuration.getProperty(IConfiguration.RMI_NAME), this);
            incidenciaBO = new IncidenciaBO();
        } catch (AlreadyBoundException ex) {
            Logger.getLogger(GestorIncidenciesImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (AccessException ex) {
            Logger.getLogger(GestorIncidenciesImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String args[]) {
        
        try {
            GestorIncidenciesImpl obj = new GestorIncidenciesImpl(PropertiesConfiguration.getInstance());
            System.out.println("Server ready");
        } catch (Exception e) {
            System.err.println("Server exception: " + e.toString());
            e.printStackTrace();
            System.exit(1);
        }
    }

    public void printarMissatges(String miss) throws RemoteException {
        System.out.println(miss);
    }

    public boolean novaIncidencia(Request request) throws RemoteException{
        return incidenciaBO.novaIncidencia(request);
    }

    public boolean modificarIncidencia(Request request, RequestNote rqNote) throws RemoteException{
        return incidenciaBO.modificarIncidencia(request, rqNote);
    }

    public List<Request> getIncidenciesActives() throws RemoteException{
        return incidenciaBO.getIncidenciesActives();
    }

    public List<Request> getIncidenciesResoltes() throws RemoteException{
        return incidenciaBO.getIncidenciesResoltes();
    }

    public List<Request> getIncidencies(Date date) throws RemoteException{
        return incidenciaBO.getIncidencies(date);
    }

    public boolean resoldreIncidencia(Request request) throws RemoteException{
        return incidenciaBO.resoldreIncidencia(request);
    }

    public String sayHello() throws RemoteException{
        return "Hello, world!";
    }

    public List<HistoricIncidenciaTO> getHistorics(RequestType requestType) throws RemoteException{
        return null;
    }

    public List<User> getUsers() throws RemoteException {
        return incidenciaBO.getUsers();
    }

    public List<Area> getAreas() throws RemoteException {
        return incidenciaBO.getAreas();
    }

    public boolean resoldreIncidencia(Integer idRequest, Integer idUserNote, String userNote) throws RemoteException{
        return incidenciaBO.resoldreIncidencia(idRequest, idUserNote, userNote);
    }

    public List<RequestNote> getRequestNoteFromRequest(Request request) throws RemoteException {
        return incidenciaBO.getRequestNoteFromRequest(request);
    }
}
